﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using Wei8808Cms.BLL;
using Wei8808Cms.Model;
using Wei8808Cms.WebSite.UI;

namespace Wei8808Cms.WebSite.admin.Case
{
    public partial class CaseMasteRForm : BasicPage
    {
        public int pcount;                                   //总条数
        public int page;                                     //当前页
        public readonly int pagesize = 14;                    //设置每页显示的大小

        private readonly CaseBLL _bll = new CaseBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                chkLoginLevel("viewCase");
                this.RptBind();
            }
        }

        private void RptBind()
        {
            if (!int.TryParse(Request.Params["page"] as string, out this.page))
            {
                this.page = 0;
            }
            var qcmodel = new Pagination();
            qcmodel.PageSize = pagesize;
            qcmodel.PageIndex = page;
            this.pcount = _bll.GetCount();
            this.lbtnDel.Enabled = this.pcount > 0;
            this.rptList.DataSource = _bll.GetPageList(qcmodel);
            this.rptList.DataBind();
        }

        protected void lbtnDel_Click(object sender, EventArgs e)
        {
            chkLoginLevel("delCase");
            var list = new List<CaseInfo>();
            for (var i = 0; i < rptList.Items.Count; i++)
            {
                var id = ((Label)rptList.Items[i].FindControl("lb_id")).Text;
                var cb = (CheckBox)rptList.Items[i].FindControl("cb_id");
                if (!cb.Checked) continue;
                var model = new CaseInfo { CaseId = int.Parse(id) };
                list.Add(model);
            }
            if (_bll.Delete(list))
            {
                JscriptPrint("删除成功！", "CaseMasterForm.aspx", "Success");
            }
            else
            {
                JscriptPrint("删除失败！", "CaseMasterForm.aspx", "Error");
            }
        }
        protected void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int id = Convert.ToInt32(((Label)e.Item.FindControl("lb_id")).Text);
            var model = _bll.GetCaseInfo(id);
            switch (e.CommandName.ToLower())
            {
                case "ibtnrec":
                    _bll.UpdateField(id, model.IsRec == 1 ? " IsRec=0 " : " IsRec=1 ");
                    break;
            }
            this.RptBind();
        }
    }
}